<?php

namespace App\Models;

/**
 * Class EarningsLog
 * @property integer id
 * @property integer user_id 用户ID
 * @property integer from_user_id 消费用户ID
 * @property integer detail 详情
 * @property integer price 金额
 * @property integer type 类型，1:收入，2:支出
 * @property integer purchase_id 订单ID
 * @property integer product_id 商品ID
 * @property float service_charge 平台服务费
 * @property string source 来源
 * @property User fromUser 用户信息
 * @property User user 用户信息
 */
class EarningsLog extends ApplicationModel
{
    protected $guarded = [];
    const TYPE_IN = 1; //收入
    const TYPE_OUT = 2; //支出
    const TYPE_PAY = 3;


    public function fromUser()
    {
        return $this->belongsTo(User::class, 'from_user_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 验证之前是否有记录
     * @return bool
     */
    public static function valid_double($purchase_id, $user_id, $from_user_id, $type, $source)
    {
        return EarningsLog::where('purchase_id', $purchase_id)->where('user_id', $user_id)->where('from_user_id', $from_user_id)->where('type', $type)->where('source', $source)->count();
    }
}
